package com.atwisdom.star.core.dao.pojo.modelClass.logic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.core.dao.pojo.modelClass.BaseModel;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierModel;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierPropertiesUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * @TableName logic_model
 */
@TableName(value = "logic_model", autoResultMap = true)
@Data
@NoArgsConstructor
public class LogicModel extends BaseModel {

    /**
     * 工艺路线模型对应的容器模型列表
     * 使用前需要先设置
     */
    @TableField(exist = false)
    private List<HierModel> hierModels;
    /**
     * 容器属性信息
     */
    @TableField(exist = false)
    private JSONObject hierProperties;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /**
     * 根据容器模型列表获取HierProperties;
     *
     * @return 返回构造好的容器结构
     */
    public JSONObject getHierProperties() {
        if (this.hierProperties == null || this.hierProperties.size() < 1) {
            HierPropertiesUtils hierProps = new HierPropertiesUtils(hierModels);
            this.hierProperties = hierProps.fetchHierProperties();
        }
        return this.hierProperties;
    }

    /**
     * 模型对应的容器属性
     *
     * @param hierProperties 容器属性
     */
    public void setHierProperties(JSONObject hierProperties) {
        this.hierProperties = hierProperties;
    }

    /**
     * 根据类型、类名实例化工艺路线模型，
     * 此时容器属性为空；
     *
     * @param type      类型
     * @param className 类名
     */
    public LogicModel(String type, String className) {
        this.type = type;
        this.className = className;
        this.version = new BigDecimal("1.0");
        this.published = true;
        this.comments = "";
        this.setProperties(new JSONArray());
        this.setHierProperties(new JSONObject());
        this.setStatusProperties(new JSONArray());
        this.setWeb(new JSONObject());
        this.setOthers(new JSONObject());
    }

    /**
     * 有参构造方法
     *
     * @param jsonObject 参数
     */
    public LogicModel(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getLong("id") : null;
        this.type = keys.contains("type") ? jsonObject.getString("type") : "general";
        this.className = jsonObject.getString("className");
        this.version = keys.contains("version") ? jsonObject.getBigDecimal("version") : BigDecimal.valueOf(1.0);
        this.published = keys.contains("published") ? jsonObject.getBoolean("published") : true;
        this.deleteFlag = keys.contains("deleteFlag") ? jsonObject.getBoolean("deleteFlag") : false;
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : "";
        this.web = keys.contains("web") ? jsonObject.getJSONObject("web") : new JSONObject();
        this.others = keys.contains("others") ? jsonObject.getJSONObject("others") : new JSONObject();
        JSONArray properties = keys.contains("properties") ? jsonObject.getJSONArray("properties") : new JSONArray();
        this.setProperties(properties);
        JSONArray statusProperties = keys.contains("statusProperties") ? jsonObject.getJSONArray("statusProperties") : new JSONArray();
        this.setStatusProperties(statusProperties);
    }

    /**
     * 将模型对象转成jsonObject
     *
     * @return 返回对象jsonObject
     */
    public JSONObject fetchJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.getId());
        JsonObject.put("type", this.getType());
        JsonObject.put("className", this.getClassName());
        JsonObject.put("version", this.getVersion());
        JsonObject.put("published", this.getPublished());
        JsonObject.put("web", this.getWeb());
        JsonObject.put("others", this.getOthers());
        JsonObject.put("comments", this.getComments());
        JsonObject.put("deleteFlag", this.getDeleteFlag());
        JsonObject.put("createdBy", this.getCreatedBy());
        JsonObject.put("createTime", this.getCreateTime());
        JsonObject.put("updatedBy", this.getUpdatedBy());
        JsonObject.put("updateTime", this.getUpdateTime());
        JsonObject.put("properties", this.getProperties());
        JsonObject.put("hierProperties", this.getHierProperties());
        JsonObject.put("statusProperties", this.getStatusProperties());
        return JsonObject;
    }

    /**
     * 将模型对象转成jsonObject
     *
     * @return 返回对象jsonObject
     */
    public JSONObject fetchInfoJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.getId());
        JsonObject.put("type", this.getType());
        JsonObject.put("className", this.getClassName());
        JsonObject.put("version", this.getVersion());
        JsonObject.put("published", this.getPublished());
        JsonObject.put("others", this.getOthers());
        JsonObject.put("comments", this.getComments());
        JsonObject.put("createdBy", this.getCreatedBy());
        JsonObject.put("createTime", this.getCreateTime());
        JsonObject.put("updatedBy", this.getUpdatedBy());
        JsonObject.put("updateTime", this.getUpdateTime());
        return JsonObject;
    }
}